//index.js
//获取应用实例
import { GETINDEXDATA, USERLOGIN } from "../../utils/api";
import { ajax } from "../../utils/httpRequest";
var timer;
Page({
  data: {
    day: 0,
    hr: 0,
    min: 0,
    showMask: false,
    showLoginGetWord:false
  },
  onLoad: function () {
    this.countdown();
    this.initData()
  },
  initData() {
    ajax.get(GETINDEXDATA, {}).then(res => {
      console.log(res.code);
      // this.indexData = res.data;
      // this.word_puzzles = res.data.word_puzzles;
      // // this.countdown(this.formatDate(res.data.end_time))
      // this.countdown(res.data.end_time);
    });
  },
  login() {
    wx.login({
      success(res) {
        if (res.code) {
          ajax.get(USERLOGIN, { code: res.code }).then(res => {
            console.log(res);
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  countdown(endtime) {
    const testtime = '2019-03-09 16:45';
    const end = new Date(testtime.replace(/-/g, '/')).getTime();
    const now = new Date().getTime();
    const msec = end - now;
    if (msec > 0) {
      let day = parseInt(msec / 1000 / 60 / 60 / 24);
      let hr = parseInt(msec / 1000 / 60 / 60 % 24);
      let min = parseInt(msec / 1000 / 60 % 60);
      let sec = parseInt(msec / 1000 % 60);
      this.setData({ day: day, hr: hr > 9 ? hr : '0' + hr, min: min > 9 ? min : '0' + min });
      // this.day = day;
      // this.hr = hr > 9 ? hr : '0' + hr;`
      // this.min = min > 9 ? min : '0' + min;
      // this.sec = sec > 9 ? sec : '0' + sec;
    } else {
      clearTimeout(timer);
      return
    }
    timer = setTimeout(() => {
      this.countdown()
    }, 60000)
  },
  cancelclick() {
    this.setData({
      showMask: false,
      showLoginGetWord:false
    })
  },
  getWordclick() {
    this.setData({
      showMask: true
    })
  },

});
